package org.example.config;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.handler.SecurityFailureHandler;
import org.example.handler.SecuritySuccessHandler;
import org.example.service.UserDetailsServiceImpl;
import org.example.service.UserService;
import org.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.parameters.P;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;

@Configuration
@EnableWebSecurity
public class TestConfig {
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer(){
        return (web->web.ignoring().requestMatchers("/js/**","/images/**","/css/**"));
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(ahr->
                ahr.requestMatchers("/login").permitAll()
                        .anyRequest().authenticated()
        );
//        http.userDetailsService(userService);
        http.csrf().disable();
        return http.build();
    }


    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Bean
    public AuthenticationManager authenticationManager(PasswordEncoder passwordEncoder){
        DaoAuthenticationProvider provider=new DaoAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService);
        provider.setPasswordEncoder(passwordEncoder);
        ProviderManager providerManager=new ProviderManager(provider);
        return providerManager;
    }




















//    @Bean
//    PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
//
//    @Bean
//    WebSecurityCustomizer webSecurityCustomizer(){
//        return (web -> web.ignoring().requestMatchers("/js/**"));
//    }
//
//    @Bean
//    public UserDetailsService userDetailsService(){
//        UserDetails user1= User.withUsername("user1").password(passwordEncoder().encode("123"))
//                .roles("roles1").build();
//        return new InMemoryUserDetailsManager(user1);
//    }
//
//    @Bean
//    SecurityFilterChain securityFilterChain(HttpSecurity http)throws Exception{
//        http.authorizeHttpRequests(ahr->
//                ahr.requestMatchers("/login").permitAll()   //login页可匿名访问
//                        .anyRequest().authenticated()   //其他请求需要授权
//        );
//        http.formLogin(formLogin->
//                    formLogin
//                        .loginProcessingUrl("/login")   //提交表单
//                            .successHandler(new SecuritySuccessHandler())
//                            .failureHandler(new SecurityFailureHandler())
//        );
//        http.csrf().disable();  //跨域漏洞防御关闭
//        return http.build();
//    }

}
